package org.example.facade.panel;

import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import org.example.physics_engine.body.PhysicsBody;
import org.example.physics_engine.body.Transform;
import org.example.physics_engine.geometry.Vector2d;
import org.example.physics_engine.geometry.shapes.AbstractShape;
import org.example.physics_engine.geometry.shapes.Circle;
import org.example.physics_engine.geometry.shapes.Polygon;

public class DefaultPanelRenderer implements PanelRenderer{
    private static final Color FILL_COLOR = Color.valueOf("#1296db");

    //单例模式
    public static final DefaultPanelRenderer INSTANCE = new DefaultPanelRenderer();

    @Override
    public void drawToCanvas(GraphicsContext graphicsContext, PhysicsBody physicsBody) {
        AbstractShape abstractShape = physicsBody.getShape();
        Transform transform = abstractShape.getTransform();
        //画多边形
        if(abstractShape instanceof Polygon){
            Polygon polygon = (Polygon) abstractShape;
            graphicsContext.setFill(FILL_COLOR);
            Vector2d[] vector2ds = polygon.getVertices();
            double[] xpoints = new double[vector2ds.length];
            double[] ypoints = new double[vector2ds.length];
            for(int i = 0; i<vector2ds.length; i++){
                Vector2d transforemed = transform.getTransformed(vector2ds[i]);
                xpoints[i] = transforemed.x;
                ypoints[i] = transforemed.y;
            }
            graphicsContext.fillPolygon(xpoints,ypoints,vector2ds.length);
        }
        else if (abstractShape instanceof Circle){
            Circle circle = (Circle) abstractShape;
            graphicsContext.setFill(FILL_COLOR);
            graphicsContext.fillOval(
                    circle.getTransform().getX() - circle.getRadius(),
                    circle.getTransform().getY() - circle.getRadius(),
                    circle.getRadius()*2,
                    circle.getRadius()*2);
        }
    }
}
